import config from '@/common/config'
import { useUserStore } from '@/stores/user'

export const UPLOAD_URL = `${config.baseURL}/a/product/file/upload`

export const uploadFileByPath = filePath => {
  return new Promise((resolve, reject) => {
    const userStore = useUserStore()
    uni.uploadFile({
      url: UPLOAD_URL,
      filePath,
      name: 'file',
      header: {
        Authorization: userStore.token,
        'X-Access-Token': userStore.token,
      },
      success: res => {
        if (res.statusCode == 200) {
          resolve(JSON.parse(res.data))
        } else {
          reject(res.data)
        }
      },
      fail: err => {
        reject(err)
      }
    })
  })
}
